<template>
  <page-header-wrapper :key="key">
    <a-card :bordered="false">
      <div class="intro-wrap" style="">
        <ul>
          <li>文本短信由三部分构成: 签名 + 内容 + 变量。</li>
          <li>模板分为两种: 通知类和运营类。</li>
          <li>短信签名&模板审核时间一般为1-3个工作日,请耐心等待审核结果。</li>
          <!-- <li>短信/模板模板申请相见规范。</li> -->
        </ul>
      </div>
      <div class="table-page-search-wrapper">
        <a-form layout="inline">
          <a-row :gutter="48">
            <!-- <a-col :md="8" :sm="24">
              <a-form-item label="平台">
                <AWM-select
                  v-model="queryParam.platformId"
                  placeholder="请选择平台"
                  funStr="platformByNoAuth"
                ></AWM-select>
              </a-form-item>
            </a-col> -->
            <a-col :md="8" :sm="24">
              <a-form-item label="状态">
                <a-select v-model="queryParam.status" placeholder="请选择状态">
                  <a-select-option value="">全部</a-select-option>
                  <a-select-option v-for="it in templateStatusList" :key="it.id" :value="it.id">{{
                    it.name
                  }}</a-select-option>
                </a-select>
              </a-form-item>
            </a-col>

            <!-- <a-col :md="(!advanced && 8) || 24" :sm="24"> -->
            <a-col :md="8" :sm="24">
              <span
                class="table-page-search-submitButtons"
                :style="(advanced && { float: 'right', overflow: 'hidden' }) || {}"
              >
                <a-button type="primary" @click="handleSearch">查询</a-button>
                <a-button style="margin-left: 8px" @click="() => (this.queryParam = {})">重置</a-button>
              </span>
            </a-col>
          </a-row>
        </a-form>
      </div>
      <div class="table-operator">
        <a-button type="primary" icon="plus" @click="handleAdd">新建</a-button>
      </div>
      <s-table
        ref="table"
        size="default"
        rowKey="id"
        :columns="columns | columnsFilter"
        :data="loadData"
        showPagination="auto"
        :scroll="{ x: true }"
        :pagination="{ 'show-total': (total) => `Total ${total} items` }"
      >
        <span slot="action" slot-scope="text, record">
          <template >
            <span  v-if="record.status == 3">
              <a @click="handleEdit(record)">编辑</a>
              <a-divider type="vertical" />
            </span>
            <span>
              <a @click="handleDel(record)">删除</a>
            </span>
          </template>
        </span>

        <span slot="status" slot-scope="text">
          <a-badge
            :color="text | statusFilter(templateStatusList, 'status')"
            :text="text | statusFilter(templateStatusList, 'name')"
          />
        </span>
        <span slot="type" slot-scope="text">
          <a-badge
            :color="text | statusFilter(templateTypeList, 'status')"
            :text="text | statusFilter(templateTypeList, 'name')"
          />
        </span>
        <span slot="templateType" slot-scope="text">
          <a-badge
            :color="text | statusFilter(template_templateTypeList_Show, 'status')"
            :text="text | statusFilter(template_templateTypeList_Show, 'name')"
          />
        </span>
        <span slot="isOversea" slot-scope="text">
          <a-badge
            :color="text | statusFilter(isOverseaList, 'status')"
            :text="text | statusFilter(isOverseaList, 'name')"
          />
        </span>
      </s-table>
    </a-card>
  </page-header-wrapper>
</template>

<script>
import moment from 'moment'
import { STable } from '@/components'
import { getPlatformTemplateList, platformTemplateUpdate } from '@/api/sms'
import { userTypeList, userStatusList, templateStatusList, templateTypeList, template_templateTypeList_Show, isOverseaList } from '@/utils/other'
import { mapGetters } from 'vuex'
import AWMSelect from '@/components/AWMSelect'
// import StepByStepModal from './modules/StepByStepModal'
// import TemplateForm from './modules/TemplateForm'

const columns = [
  {
    title: 'ID',
    dataIndex: 'id',
    key: 'id',
    width: 100,
  },
  {
    title: '名称',
    dataIndex: 'name',
    key: 'name',
    width: 120,
  },
  {
    title: '签名',
    dataIndex: 'signatureName',
    key: 'signatureName',
    width: 120,
  },
  {
    title: '类型',
    dataIndex: 'templateType',
    width: 150,
    scopedSlots: { customRender: 'templateType' },
  },
  {
    title: '发送区域',
    dataIndex: 'isOversea',
    width: 150,
    scopedSlots: { customRender: 'isOversea' },
  },
  {
    title: '内容',
    dataIndex: 'content',
    width: 250,
    customRender: (text) => text || '--',
  },
  {
    title: '使用场景',
    dataIndex: 'remark',
    width: 150,
    customRender: (text) => text || '--',
  },

  {
    title: '状态',
    dataIndex: 'status',
    width: 120,
    scopedSlots: { customRender: 'status' },
  },
  {
    title: '驳回理由',
    dataIndex: 'refuseReason',
    width: '200px',
  },
  {
    title: '添加时间',
    dataIndex: 'addTime',
    width: 180,
  },
  {
    title: '操作',
    dataIndex: 'action',
    width: '140px',
    scopedSlots: { customRender: 'action' },
  },
]

export default {
  name: 'platform-template',
  components: {
    STable,
    AWMSelect,
    // TemplateForm,
  },
  data() {
    // this.columns = columns
    return {
      value: 1,
      projectTypeList: [],
      projectTypeList1: [],
      pathName: '',
      columns,
      timeout: null,
      inviteUserList: [],
      fetching: false,
      // create model
      visible: false,
      confirmLoading: false,
      mdl: null,

      // 高级搜索 展开/关闭
      advanced: false,
      // 查询参数
      parameter: {
        pageNum: 1,
        pageSize: 10,
      },
      queryParam: {},
      // 状态
      userStatusList,
      //  角色类型
      userTypeList,
      // 性别
      templateStatusList,
      templateTypeList,
      template_templateTypeList_Show,
      isOverseaList,
      // 排序
      // 加载数据方法 必须为 Promise 对象
      loadData: (parameter) => {
        console.log(parameter)
        const requestParameters = Object.assign(parameter, this.queryParam, this.computedQueryParam)
        return getPlatformTemplateList(requestParameters).then((res) => {
          this.parameter.pageNum = parameter.pageNo
          let result = {
            data: res.data.data,
            pageNo: parameter.pageNo,
            pageSize: parameter.pageSize,
            totalCount: res.data.total,
            totalPage: res.data.pages,
          }
          return result
        })
      },

      table: {
        data: [],
        loading: false,
        // pagination:{pageSize: 10, current: 1},
        pagination: {},
        columns: [],
        filters: null,
        sorter: null,
        key: 0,
        current: 1,
      },
      selectedRowKeys: [],
      selectedRows: [],
      key: 0,
    }
  },
  filters: {
    columnsFilter: function (a) {
      if (a) {
        return a.filter((v) => v.check || v.check == undefined)
      } else {
        return []
      }
    },
  },
  created() {
    this.parameter.pageNum = this.$route.query.pageNum
  },
  computed: {
    isInternational: function () {
      return this.$route.path.indexOf('/international/') != -1 ? 2 : 1
    },
    computedQueryParam: function () {
      return {
        isInland: this.isInternational,
      }
    },
  },
  methods: {
    handleSearch() {
      this.$refs.table.refresh(true)
    },
    handleAdd() {
      this.$router.push({
        path: `/platformTemplate/add`,
        query: { pageNum: this.parameter.pageNum },
      })
    },
    handleEdit(record) {
      this.$store.dispatch('platformTemplateToUpdate', record).then((res) => {
        this.$router.push({
          path: `${this.isInternational == 2 ? '/international' : '/inland'}/platformTemplate/update/${record.id}`,
          query: { pageNum: this.parameter.pageNum },
        })
      })
      //   this.visible = true
      //   this.mdl = { ...record }
    },
    handleCancel() {
      this.visible = false
      const form = this.$refs.createModal.form
      form.resetFields() // 清理表单数据（可不做）
    },
    handleDel(a_record) {
      let self = this
      const modal = self.$confirm({
        title: '模板删除',
        content: `是否删除模板:${a_record.name}`,
        okText: '确认',
        cancelText: '取消',
        onOk: () => self.handleDelete(a_record.id, modal, -1),
        onCancel: () => {},
      })
    },
    handleDelete(a_id, a_modal, a_type) {
      return new Promise((resolve, reject) => {
        let data = { id: a_id, status: a_type }
        platformTemplateUpdate(data)
          .then((res) => {
            this.$refs.table.refresh()
            this.$message.success('删除成功!')
            resolve()
          })
          .catch((err) => {
            a_modal.destroy()
            this.$message.error('操作失败!')
          })
      })
    },
  },
  watch: {
    '$route.name': function (n, o) {
      this.pathName = n
      this.key++
    },
  },
}
</script>
<style scoped lang="less">
.intro-wrap {
  background: #f0f2f5;
  padding: 24px;
  margin-bottom: 24px;
  ul {
    padding-left: 5px;
    list-style: disc;
    li {
    }
  }
}
</style>